/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ocm.core.log.exception.ServiceException;
import com.ocm.warehouse.bo.StockLockBO;
import com.ocm.warehouse.bo.StockLockItemsBO;
import com.ocm.warehouse.entity.StockLock;
import com.ocm.warehouse.service.IStockLockService;
import com.ocm.warehouse.entity.Stock;
import com.ocm.warehouse.vo.StockVO;
import com.ocm.warehouse.mapper.StockMapper;
import com.ocm.warehouse.service.IStockService;
import com.ocm.core.mp.base.BaseServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 *
 * @author HQY
 * @since 2021-11-30
 */
@Service
public class StockServiceImpl extends BaseServiceImpl<StockMapper, Stock> implements IStockService {

	@Resource
	private IStockLockService stockLockService;

	@Resource
	private RabbitTemplate rabbitTemplate;

	@Override
	public IPage<StockVO> selectStockPage(IPage<StockVO> page, QueryWrapper<StockVO> stock) {
		return page.setRecords(baseMapper.selectStockPage(page, stock));
	}

	@Override
	public Boolean getStockMin(Long spuId) {
		return baseMapper.selectStockMin(spuId);
	}

	@Override
	@Transactional
	public void stockLock(StockLockBO stockLocks, Long orderId) {
		for (StockLockItemsBO s : stockLocks.getItems()) {
			if (!this.update(new UpdateWrapper<Stock>()
				.setSql("stock_lock=stock_lock+"+s.getCount())
				.eq("sku_id", s.getSkuId())
				.eq("is_deleted", 0)
				.geSql("stock_number", "stock_lock+" + s.getCount()))) {
				throw new ServiceException(s.getSkuId().toString());
			}
			stockLockService.save(new StockLock(null,orderId,s.getSkuId(),null,s.getCount(),1));
		}
		rabbitTemplate.convertAndSend("stock-event-exchange","stock.locked",stockLocks.getOrderId());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void stockUnlock(Long orderId) {
		List<StockLock> list = stockLockService.list(new QueryWrapper<StockLock>().eq("order_id", orderId).eq("status", 1));
		if(list!=null&&list.size()>0){

			for(StockLock s:list){
				boolean flag=this.update(new UpdateWrapper<Stock>()
					.setSql("stock_lock=stock_lock-"+s.getLockNum())
					.ge("stock_lock",s.getLockNum() )
					.eq("sku_id",s.getSkuId())
					.eq("is_deleted",0))&&
					stockLockService.update(new UpdateWrapper<StockLock>().eq("id",s.getId()).set("status",0));
				if(!flag){
					throw new ServiceException("取消库存锁定失败");
				}
			}

		}

	}

	@Override
	@Transactional
	public void stockReceiving(Long orderId) {
		List<StockLock> list = stockLockService.list(new QueryWrapper<StockLock>().eq("order_id", orderId));
		if(list!=null&&list.size()>0){

			for(StockLock s:list){
				boolean flag=this.update(new UpdateWrapper<Stock>()
					.setSql("stock_lock=stock_lock-"+s.getLockNum())
					.setSql("stock_number=stock_number-"+s.getLockNum())
					.ge("stock_lock",s.getLockNum())
					.ge("stock_number",s.getLockNum())
					.eq("sku_id",s.getSkuId())
					.eq("is_deleted",0))&&
					stockLockService.update(new UpdateWrapper<StockLock>().eq("id",s.getId()).set("status",2));
				if(!flag){
					throw new ServiceException("发货失败");
				}
			}

		}


	}
}
